﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTunesLib;
using MP3Ganizer.BusinessObject.DataToPersist;
using System.Diagnostics;

namespace MP3Ganizer.BusinessObject.Services.Configuration.Builders
{
    class AlbumsBuilder : AlbumsEntreListBuilder, IConfigurationBuilder
    {
        public override object GetInfo(DataToPersist.LocalConfiguration localConfiguration)
        {
            throw new NotImplementedException();
        }

        private void TryToAdd(ICollection<string> infos, string artist)
        {
            if (!string.IsNullOrEmpty(artist as string) && !infos.Contains(artist))
                infos.Add(artist);
        }

        public override void GetInfosObservable(LocalConfiguration localConfiguration, System.Collections.ObjectModel.ObservableCollection<string> infos) 
        {
            if (!localConfiguration.UseITunesLibrary)
            {
                // iTunes classes
                iTunesApp itunes = new iTunesApp();
                IITLibraryPlaylist mainLibrary = itunes.LibraryPlaylist;

                IITTrackCollection tracks = mainLibrary.Tracks;
                var myEnum = tracks.GetEnumerator();

                while (myEnum.MoveNext())
                {
                    IITFileOrCDTrack currTrack = myEnum.Current as IITFileOrCDTrack;
                    // is this a file track?
                    if (currTrack != null && currTrack.Kind == ITTrackKind.ITTrackKindFile)
                    {
                        TryToAdd(infos, currTrack.Album);
                        TryToAdd(infos, currTrack.SortAlbum);
                    }
                }
            }
        }
    }
}
